package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.TextView;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.createobject.UnitAdapter;
import com.wwwarehouse.resource_center.bean.goods.SelectionUnitBean;
import com.wwwarehouse.resource_center.fragment.maintenanceunit.NewUnitFragment;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Joseph on 2017/9/14.
 *         <p/>
 */

public class UnitFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    UnitAdapter mAdapter;
    private TextView mAddUnit;
    private List<SelectionUnitBean.EmptyIdentifierBean> tagBeanList = new ArrayList<>();
    private String ownerUkid;
    private String unitUkid;

    @Override
    protected int getContentId() {
        return R.layout.fragment_unit_objectst;
    }

    @Override
    public void onSuccess(CommonClass commonClass, int i) {
    }

    @Override
    public void onReLoad(int code) {
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);
        mAddUnit = findView(view, R.id.tv_add_unit);
        Bundle arguments = getArguments();
        if (null != arguments) {
            ownerUkid = arguments.getString("ownerUkid");
            unitUkid = arguments.getString("unitUkid");
        }
        mAddUnit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到新增单位
                Bundle bundle = new Bundle();
                bundle.putString("from", "UnitFragment");
                bundle.putString("ownerUkid", ownerUkid);
                NewUnitFragment unitFragment = new NewUnitFragment();
                unitFragment.setArguments(bundle);
                pushFragment(unitFragment);
            }
        });

    }

    public void setHeight(int height, int Width) {
        mAdapter = new UnitAdapter(mActivity, tagBeanList, height, Width,unitUkid);
        mRvContent.setAdapter(mAdapter);
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        List<SelectionUnitBean.EmptyIdentifierBean> taskUnits = b.getParcelableArrayList("data");
        int pos = b.getInt("pos") - 1;
        int num = b.getInt("num");
        for (int i = pos * num; i < num + pos * num; i++) {
            if (i >= 0 && i < taskUnits.size()) {
                tagBeanList.add(taskUnits.get(i));
            }
        }
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 4) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public boolean canScrollVertically() {
                return false;
            }

        });
        final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                observer.removeGlobalOnLayoutListener(this);
                setHeight(mRvContent.getHeight(), mRvContent.getWidth());
            }
        });
        if (mAdapter != null) {
            mRvContent.setAdapter(mAdapter);
        }
    }

}
